package com.easycar.common.util;

import com.easycar.common.exception.InitializationException;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * GSON  工具类
 */
public  final  class GsonUtil {
    private static  final  GsonUtil   INSTANCE=new GsonUtil();
    private final static Gson sGson = new Gson();

    private GsonUtil() {

    }

    public   static  GsonUtil  getInstance(){
        return  INSTANCE;

    }
    /**
     * 将object对象转成json字符串
     *
     * @param object  对象
     * @return  json 字符串
     */
    public  String GsonString(Object object) {
        String gsonString = null;
        if (sGson != null) {
            gsonString = sGson.toJson(object);
        }
        return gsonString;
    }




    /**
     * 将gsonString转成泛型bean
     *
     * @param gsonString
     * @param cls
     * @return
     */
    public  <T> T GsonToBean(String gsonString, Class<T> cls) {
        T t = null;
        if (sGson != null) {
            t = sGson.fromJson(gsonString, cls);
        }
        return t;
    }

    public  <T> T GsonToBean(String gsonString,Type type) {
        T   t=null;
        if (sGson != null) {
           t = sGson.fromJson(gsonString, type);
        }
        return t;
    }

    /**
     * 转成list
     * 泛型在编译期类型被擦除导致报错
     * @param gsonString
     * @param cls
     * @return
     */
    public  <T> List<T> GsonToList(String gsonString, Class<T> cls) {
        List<T> list = null;
        if (sGson != null) {
            list = sGson.fromJson(gsonString, new TypeToken<List<T>>() {
            }.getType());
        }
        return list;
    }
    /**
     * 转成list
     * 解决泛型问题
     * @param json
     * @param cls
     * @param <T>
     * @return
     */
    public  <T> List<T> jsonToList(String json, Class<T> cls) {
        Gson gson = new Gson();
        List<T> list = new ArrayList<T>();
        JsonArray array = new JsonParser().parse(json).getAsJsonArray();
        for(final JsonElement elem : array){
            list.add(gson.fromJson(elem, cls));
        }
        return list;
    }

    /**
     * 转成list中有map的
     *
     * @param gsonString
     * @return
     */
    public  <T> List<Map<String, T>> GsonToListMaps(String gsonString) {
        List<Map<String, T>> list = null;
        if (sGson != null) {
            list = sGson.fromJson(gsonString,
                    new TypeToken<List<Map<String, T>>>() {
                    }.getType());
        }
        return list;
    }


    /**
     * 转成map的
     *
     * @param gsonString
     * @return
     */
    public  <T> Map<String, T> GsonToMaps(String gsonString) {
        Map<String, T> map = null;
        if (sGson != null) {
            map = sGson.fromJson(gsonString, new TypeToken<Map<String, T>>() {
            }.getType());
        }
        return map;
    }







}
